#!/bin/env python  # pylint: disable=invalid-name
# -*- coding: utf-8 -*-


"""
File Name: map-hbase_client_demo.py
Author: morre <morre@vip.qq.com>
Create Time: 2022/12/31 16:29:29
Brief:
"""

import sys
import json
import logging
import argparse
from hbase_client import HbaseClient


def main():
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s  %(filename)s[line:%(lineno)d] %(levelname)s "
        "%(message)s",
        datefmt="%a, %d %b %Y %H:%M:%S")

    parser = argparse.ArgumentParser(
        description="map script of hbase_client_demo")

    parser.add_argument("--hbase_conf", dest="hbase_conf_file",
                        default="hbase.conf", help="hbase config file")

    parser.add_argument("--table_name", dest="table_name",
                        default="test:test_table", help="hbase table_name")

    parser.add_argument("--db_path", dest="db_path",
                        default="hbase_meta.sqlite", help="db path")

    options = parser.parse_args(sys.argv[1:])

    obj = {}
    with open(options.hbase_conf_file, "r", encoding="utf8") as fp:
        obj = json.load(fp)

    client = HbaseClient(
        zkquorum=obj["zkquorum"], zk_root_path=obj["zk_root_path"],
        init_socket_pool_size=1, establish_connection_timeout=60,
        missing_znode_retries=3, rpc_socket_timeout=60,
        max_write_buff_size=1000)
    client.open()
    # hadoop上一定先加载hbase_meta.sqlite文件，减少对hbase meta的高并发访问。
    client.load_meta_from_sqlite(options.db_path)

    for line in sys.stdin:
        fields = line.strip("\n").split("\t")
        if not fields:
            continue
        row = client.get(options.table_name.encode(), fields[0].encode())
        if not row:
            row = {b"data:url": b"null"}
        print("\t".join(fields + [row.get(b"data:url", b"null").decode()]))


if __name__ == "__main__":
    sys.exit(main())
